/**
 * Created by novem on 2015/10/21.
 */
function pi(){
    return  3.1415926535898;
}
pow=Math.pow;
sin=Math.sin;
cos=Math.cos;
asin=Math.asin;
min=Math.min;
sqrt=Math.sqrt;
window.onload=function(){
    var rcarId = $.getUrlParam("rcarId");
    var url=getUrl();
    var userid=getUserId();
    var loc = getLoc();
    $("#jubao").attr('href','jubao.html?objId='+rcarId+"&type=4");

    $.jsonp({
        url: 'http://'+url+'/api/rcar/detail-'+rcarId,
        data: {userId:userid},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#chat").attr("onclick","easeMobGoChat("+data.telphone+")");
            $(".bacim").attr('style','background-image:url('+data.photos[0]+')');
            $("#telphone").attr("href","tel:"+data.telphone);
            var complied_tpl = juicer($("#user_tpl").text());
            var html = complied_tpl.render(data);
            $("#car_detail").append(html);
            var complied_tpl01 = juicer($("#cdetail_tpl").text());
            var html01 = complied_tpl01.render(data);
            $(".ui-form-display").append(html01);
            $.jsonp({
                url: 'http://'+url+'/api/rcar/comment_list-'+rcarId,
                data: {},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $("#all_pj").attr("href","car_allpingjia.html?rcarId="+ rcarId);
                    var len = data.arrays.length-1;
                    if(len<0){
                        var html = "";
                    }else {
                        //var unixTimestamp = new Date(data.arrays[0].createTime * 1000);
                        //data.arrays[0].createTime = unixTimestamp.toLocaleString();
                        var html = '<li class="ui-border-b"><div class="ui-comment-list-detail clearfix"><div class="ui-comment-list-user clearfix"><div class="ui-comment-list-avatar">' +
                            '<img src="' + data.arrays[0].photo + '" width="40" height="40" alt=""></div>' +
                            '<div class="ui-comment-list-info"><p>' + data.arrays[0].nickName + '</p><p class="ui-txt-muted">' + getTime(data.arrays[0].createTime) + '</p></div></div>' +
                            '<div class="ui-comment-list-star"><span class="ui-star ui-star-' + data.arrays[0].star + '"></span>' + data.arrays[0].star + '分</div></div>' +
                            '<div class="ui-comment-list-content">' +
                            '' + data.arrays[0].comment + '</div></li></ul>';
                    }
                    $("#number").text(data.totalCount + "人评价");
                    $(".ui-comment-list").append(html);
                },
                error: function (xOptions, textStatus) {}
            });
            $.jsonp({
                url: 'http://'+url+'/api/collect/collect_rcar_list',
                data: {userId:userid},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    console.log(data)
                    $.each(data.arrays,function(index,ele){
                        earthRadius = 6371;
                        var lat0 = (loc.lat * pi()) / 180;
                        var lng0 = (loc.lng * pi()) / 180;
                        var lat1 = (ele.rcarDimensions * pi()) / 180;
                        var lng1 = (ele.rcarLongitude * pi()) / 180;
                        calcLongitude = lng1 - lng0;
                        calcLatitude = lat1 - lat0;
                        stepOne = pow(sin(calcLatitude / 2), 2) + cos(lat0) * cos(lat1) * pow(sin(calcLongitude / 2), 2);
                        stepTwo = 2 * asin(min(1, sqrt(stepOne)));
                        calculatedDistance = earthRadius * stepTwo;
                        ele.distance=calculatedDistance.toFixed(1);
                        $("#distance").text(ele.distance);
                        var samewayId = ele.rcarId;
                        if(samewayId==rcarId){
                            $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
        },
        error: function (xOptions, textStatus) {
        }
    });
    //收藏
    $(document).on('click','.w_shac',function(){
        $.jsonp({
            url: 'http://'+url+'/api/collect/collect_rcar',
            data: {rcarId:rcarId,userId:userid},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var code = data.code;
                if(code==1) {
                    $(".sc").removeClass("btn").text("收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("取消收藏成功")
                }else{
                    $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("收藏成功")
                }
            },
            error: function (xOptions, textStatus) {
            }
        });

    });
}
function getTime(t){
    var nt = t*1000;
    var n = new Date(nt);
    return formatDate(n);
}
function formatDate(now)   {
    var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    var   date=now.getDate();
    return   year+"-"+(month>=10?month:"0"+month)+"-"+(date>=10?date:"0"+date);
}
